package com.csc.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.csc.constants.Constants;
import com.csc.domain.Account;
import com.csc.domain.Customer;
import com.csc.domain.Transaction;
import com.csc.service.AccountService;
import com.csc.service.CustomerService;
import com.csc.service.TransactionService;
import com.csc.service.UserService;

@Controller
public class BaseController {

	@Autowired
	private TransactionService transactionService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CustomerService customerService;
	
	static final Logger logger = Logger.getLogger(BaseController.class);
	
	Date from = null, to = null;
	String stringDateFrom, stringDateTo;
	String stringIdCard;

	
	/*author: Hoang Hung*/
	@RequestMapping(value="/viewTransaction")
	public String doViewTransaction() {
		logger.info("go to view transaction");
		return "viewTransaction";
	}

	/*author: Hoang Hung*/
	@RequestMapping(value = "/searchTransaction", method = RequestMethod.POST)
	public String searchTransaction(Model model, HttpServletRequest request) {
		
		// get input IdCardNumber
		stringIdCard = request.getParameter("idcard");
		//get from date
		stringDateFrom = request.getParameter("fday");
		// get to date
		stringDateTo = request.getParameter("tday");
		String url = null;
		
		//ModelAndView modelAndView = new ModelAndView();
			
			try {
				// format input date by yyyy-MM-dd hh:mm:ss
				from = new SimpleDateFormat(Constants.SIMPLE_FORMAT_DATE)
						.parse(stringDateFrom + " 00:00:00");
				to = new SimpleDateFormat(Constants.SIMPLE_FORMAT_DATE)
				.parse(stringDateTo + " 23:59:59");
			}
			catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//get list customer by id card
			Customer tempCustomer = customerService.searchCustomerByIDCard(stringIdCard);
			
			if ("".equals(stringIdCard)) {
			
				model.addAttribute("error", "Please Input Id card!");
				url = "viewTransaction";
				
			}
			else if (tempCustomer == null) {
				
				model.addAttribute("error", "ID Card is not exist!");
				url = "viewTransaction";
				
			} else {
			
			
				String idCardNumber = tempCustomer.getIdCardNumber();
	
				// show list account
				List<Account> accountList = accountService.getListAccountByIdCardActive(idCardNumber);
				
				logger.info("searchIDCard" + accountList);
			
				model.addAttribute("viewAccounts", accountList);
				url = "viewTransaction";
			
			}

		
		return url;
	}

	/*author: Hoang Hung*/
	@RequestMapping(value = "/viewTransactionByAccountId", method = RequestMethod.GET)
	public String viewTransaction(HttpServletRequest request, Model model,
			@RequestParam("accountId") Integer accountId,
			HttpServletResponse response) {
		logger.info("accountId " + accountId);
		String url = null;
		
		String message;
		List<Transaction> viewTransactions = null;
		logger.info("/// "+accountId+ "view transaction is called...");
		
		Customer tempCustomer = customerService.searchCustomerByIDCard(stringIdCard);
		
		String idCardNumber = tempCustomer.getIdCardNumber();
		
		// show list account
		List<Account> accountList = accountService.getListAccountByIdCardActive(idCardNumber);
		
		logger.info("searchIDCard" + accountList);
		
		if (stringDateFrom.equals("") || stringDateTo.equals("")) {
			
			viewTransactions = transactionService.getTransactionListByAccountId(accountId);
		}
		else {
			
			viewTransactions = transactionService.getTransactionListByCustomer(from, to, accountId);
		}
		//show account
			model.addAttribute("viewAccounts",accountList);
		if (viewTransactions.size() <= 0) 
			  
			 
			
		{
			message = "Account can not transaction !";
			// show message
			model.addAttribute("message", message);
			return "viewTransaction";
		}
		message = "View Transaction successfully";
		
		// show message
		model.addAttribute("message", message);
		
		//show list transaction
		model.addAttribute("viewTransactions",viewTransactions);
		url ="viewTransaction";
		return url;

	}
	

	/*
	 * @author:  Hoang Hung*/
	@RequestMapping(value="/searchAccount")
	public String doSeachAccount(){
		return "searchAccount";
	}
	
	/*
	 * @author:  Hoang Hung*/
	@RequestMapping(value = "/searchIDCard",method= RequestMethod.POST )
	public String searchIDCard(HttpServletRequest request, Model model) {
		//search customer by ID card
		String url;
		// create a session
		HttpSession session = request.getSession();
		//get information input ID card
		String idCard = request.getParameter("txtSearchAccount");
		
		logger.info("input idcard: " + idCard);
		//get list customer by ID card
		Customer tempCustomer = customerService.searchCustomerByIDCard(idCard);
		
		logger.info("list customer " + tempCustomer);
		
		if ("".equals(idCard)) {
			logger.info("idCard is null");
			// return error to view
			model.addAttribute("error", "Please Input Id card!");
			
			url="searchAccount";
		}
		else if (tempCustomer == null) {
			
			logger.info("tempCustomer is null");
			// return error to view
			model.addAttribute("error", "ID Card is not exist!");
			
			url="searchAccount";
		} else {
	
			// get id card number in list customer
			String idCardNumber = tempCustomer.getIdCardNumber();
			
			logger.info("ID Card Number : " + idCardNumber);
			// show list account
			List<Account> accountList = accountService.getAccountListByIdCardNumber(idCardNumber);
			
			session.setAttribute("accountIdCard", accountList);
			logger.info("searchIDCard" + accountList);
			// get accountList to view
			model.addAttribute("accounts", accountList);
			url="searchAccount";
		}
		return url;
	}
	
	/*
	 * @author:  HoangHung*/
	@RequestMapping(value="/detailAccount", method= RequestMethod.GET)
	public String doDetailAccount(Model model, @RequestParam("accountId") String accountId,HttpServletRequest request) {
		//get account information
		Account account = null;
		try {
			account = accountService.findAccountInfoById(Integer.parseInt(accountId));
		} catch (Exception e) {
			model.addAttribute("error", "Account id not valid");
		}
		
		if (account == null) {
			model.addAttribute("error", "Account not found");
		}
		 
		//show account list
		model.addAttribute("acc",account);
		return "detailAccount";
	}
	
	// TrangNguyen
	@RequestMapping(value="/detailAccountNumber", method= RequestMethod.GET)
	public String doDetailAccountNumber(Model model, @RequestParam("accountNumber") String accountNumber,HttpServletRequest request) {
		//get account information
		Account account = accountService.findAccountInfoByAccountInfoNumber(accountNumber);
		if (account == null) {
			model.addAttribute("error", "Account not found");
		}
		//show account list
		model.addAttribute("acc",account);
		model.addAttribute("closeAdmin", "closeAdmin");
		return "detailAccount";
	}
	/*
	 * @author:  Hoang Hung*/
	@RequestMapping(value="/backSearchAccount", method= RequestMethod.GET)
	public String backSearchAccount(Model model,HttpServletRequest request) {
		
		HttpSession session = request.getSession();
		// get account list in session
		List<Account> accountList = (List<Account>) session.getAttribute("accountIdCard");
		
		//show account list
		model.addAttribute("accounts",accountList);
		return "searchAccount";
	}
	


}
